package com.example.lab05.controlller;

import com.example.lab05.entity.Member;
import com.example.lab05.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("member")
public class MemberController extends BaseController{

    private final MemberService memberService;

    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @GetMapping("queryAll")
    @ResponseBody
    public List<Member> queryAll(){
        return memberService.queryAll();
    }
    @PostMapping("create")
    @ResponseBody
    public boolean create(@RequestPart MultipartFile file,@RequestPart Member member) throws IOException {
        File directory = new File("D:\\lab\\avatar");
        String extension = StringUtils.getFilenameExtension(file.getOriginalFilename());//后缀名
        String filename = UUID.randomUUID()+"."+extension;
        file.transferTo(new File(directory,filename));//保存
        member.setAvatar(filename);
        memberService.create(member);
        return true;
    }
    @PostMapping("queryAccount")
    @ResponseBody
    public Member queryAccount(@RequestBody Map<String,String> map){
        Member member = memberService.query("queryAccount",map);;
        session.setAttribute("member",member);
        return member;
    }
}
